var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var x = 0
var y = 0

function getMouseXY(e) {
if(IE){
x = event.clientX + document.body.scrollLeft;
y = event.clientY + document.body.scrollTop;
}
else{
x = e.pageX;
y = e.pageY;
}
if (x < 0){ x = 0; }
if (y < 0){ y = 0; }

return y + "," + x;
}

function debugit(txt){

document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + txt + "<br>";

}



function show(id){

if(id == "int"){

for(var i = 0;i<document.getElementsByTagName("span").length;i++){

document.getElementsByTagName("span")[i].style.display = "none";

}

document.getElementById("nav").style.display = "block";
document.getElementById("main").style.display = "block";
if(getIP() == 0){ document.getElementById("imagepack").value = "http://static.pardus.at/images/"; setCookie("PST_config","http://static.pardus.at/images/|1"); }
else{ document.getElementById("imagepack").value = getIP(); }
document.getElementById("dblevel").value = getDBL();

}

else{

for(var i = 0;i<document.getElementsByTagName("span").length;i++){

document.getElementsByTagName("span")[i].style.display = "none";

}

document.getElementById("nav").style.display = "block";
document.getElementById(id).style.display = "block";

}

document.getElementById("menu").innerHTML = '';
document.getElementById("locked").innerHTML = 'no';
if(id=='comm'){ dycom(1); }
document.getElementById("searchresults").innerHTML = '';

}

function searchSectors(){

var n = '<br>Search results:<br><table>';
var sdata = document.getElementById("ssdata").value;
sdata = sdata.toLowerCase();
var stemp = new Array();
var count = 0;

for(var i = 0;i<sectors.length;i++){

var temp = sectors[i].toLowerCase();
if(temp.search(sdata)!=-1){

stemp[stemp.length] = sectors[i]+"|"+i;
count = count + 1;

}

}

if(count == 0){

n = "<br><i>no results to display</i>";

}

else{

stemp.sort();

for(var i = 0;i<stemp.length;i++){

var temp = stemp[i].split("|");
c = temp[1]*1;
s = temp[0];
n = n + "<tr><td>"+(i+1)+"</td><td>"+s+"</td><td><a href='#' onClick='loadCode("+c+")'>load sector</a></td></tr>";

}

n = n + "</table>";

}

document.getElementById("searchresults").innerHTML = n;

}

function loadCode(id){

document.getElementById("mapcode").value = mapcode[id];
getMapFromCode(document.getElementById("mapcode").value,"place",0);
getMap();
document.getElementById("searchresults").innerHTML = '';

}

function locked(y){

if(y){ document.getElementById("locked").innerHTML = 'yes'; }
else{ document.getElementById("locked").innerHTML = 'no'; }

}

function dycom(multi){

var compro = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var comcun = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var balance = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

for(var i = 0;i<document.getElementsByTagName("td").length;i++){

if(document.getElementsByTagName("td")[i].getAttribute("onClick")!=null){

temp = document.getElementsByTagName("td")[i].innerHTML;

if(temp.search("transparent")==-1){

var level = document.getElementsByTagName("td")[i].childNodes[1].innerHTML*1;

for(var h = 0;h<buildings.length;h++){

if(buildings[h][0] == document.getElementsByTagName("td")[i].childNodes[0].getAttribute("title")){ bnum = h; break; }

}
var bname = document.getElementsByTagName("td")[i].childNodes[0].getAttribute("title");

for(var h = 0;h<buildings[bnum][1][2][0].length;h++){


cnm = cnum[buildings[bnum][1][2][1][h]];
if(bname.search("Planet")!=-1||bname.search("Starbase")!=-1){ comcun[cnm] = comcun[cnm] + (Math.round((level/1000)*buildings[bnum][1][2][0][h]))*2; }
else{ comcun[cnm] = comcun[cnm] + Math.round(buildings[bnum][1][2][0][h]*(1 + 0.4*(level-1))); }

}

for(var h = 0;h<buildings[bnum][1][3][0].length;h++){

cnm = cnum[buildings[bnum][1][3][1][h]];
if(bname.search("Planet")!=-1||bname.search("Starbase")!=-1){ compro[cnm] = compro[cnm] + (Math.round((level/1000)*buildings[bnum][1][3][0][h]))*2; }
else{ compro[cnm] = compro[cnm] + Math.round(buildings[bnum][1][3][0][h]*(1 + 0.5*(level-1))); }

}

}

}

}

var writein = "<table><tr><th>Commodity</th><th>Produced</th><th>Consumed</th><th>Balance</th></tr>";

for(var i = 0;i<compro.length;i++){

balance[i] = (compro[i]-comcun[i])*multi;
if(balance[i]>0){ balance[i] = "<font color='green'>"+balance[i]+"</font>"; }
else{ if(balance[i]<0){ balance[i] = "<font color='red'>"+balance[i]+"</font>"; } }
writein = writein + "<tr><td>"+cname[i]+"</td><td>"+(compro[i]*multi)+"</td><td>"+(comcun[i]*multi)+"</td><td>"+balance[i]+"</td></tr>";

}

writein = writein + "</table>";
document.getElementById("comdat").innerHTML = writein;


}

function startPriceList(){

n = "<H3>Set price list</H3><a href='#' onClick='importPL()'>Import Price List</a><br><a href='#' onClick='exportPL()'>Export Price List</a><br><input type='button' value='Load' onClick='loadPL()'>&nbsp;<input type='button' value='Save' onClick='savePL()'>&nbsp;<input type='button' value='Delete' onClick='deletePL()'><br><table><tr><th>Commodity</th><th>Buy Price</th><th>Sell Price</th></tr>";

for(var i = 0;i<cname.length;i++){

n = n + "<tr><td>"+
cname[i]+"</td><td><input type='text' id='pl_buy_"+i+"' onBlur='setPL()' size='5' value='0'></td><td><input type='text' id='pl_sell_"+i+"' onBlur='setPL()' size='5' value='0'></td></tr>";

}

n = n + "</table>";
document.getElementById("tpl").innerHTML = n;

}

function setPL(){

var temp = document.getElementById("temppl").innerHTML.split("=");
var buy = temp[0].split("|");
var sell = temp[1].split("|");
var n = '';

for(var i = 0;i<(buy.length-1);i++){

n = n + document.getElementById("pl_buy_"+i).value + "|";

}

n = n + "=";

for(var i = 0;i<(sell.length-1);i++){

n = n + document.getElementById("pl_sell_"+i).value + "|";

}

document.getElementById("temppl").innerHTML = n;

}

function savePL(){

setCookie("PST_PL",document.getElementById("temppl").innerHTML);

}

function loadPL(){

if(getCookie("PST_PL") == 0){ alert("No price list found!"); }
else{
document.getElementById("temppl").innerHTML = getCookie("PST_PL");
var temp = getCookie("PST_PL").split("=");
var sell = temp[1].split("|");
var buy = temp[0].split("|");
buy[0] = buy[0].replace(" ","");

for(var i = 0;i<(buy.length-1);i++){

document.getElementById("pl_buy_"+i).value = buy[i];

}

for(var i = 0;i<(sell.length-1);i++){

document.getElementById("pl_sell_"+i).value = sell[i];

}

}

}

function deletePL(){

setCookie("PST_PL","");

}

function writeGPL(){

var n = "<br>How much would a <select id='PS_building'>";

for(var i = 0;i<buildings.length;i++){

if(buildings[i][0].search("Starbase")!=-1 || buildings[i][0].search("Planet")!=-1){ }

else{ n = n + "<option value='"+i+"'>"+buildings[i][0]+"</option>"; }

}

n = n + "</select> at level <input type='text' id='PS_level_1' maxlength='2' size='2' value='1'> with this price sheet<input type='button' onClick='buildingProfit()' value='???'><br><br><div id='PS_building_profit'></div>";
n = n + "<br>Totals for sector: <div id='total_profit_sector'>0</div>";

document.getElementById("gpl").innerHTML = n;

}

function buildingProfit(){

var s1 = document.getElementById("PS_building").selectedIndex;
var bnum = document.getElementById("PS_building").options[s1].value;
var level = document.getElementById("PS_level_1").value;
var compro = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var comcun = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var psheet = document.getElementById("temppl").innerHTML;

for(var h = 0;h<buildings[bnum][1][2][0].length;h++){

cnm = cnum[buildings[bnum][1][2][1][h]];

comcun[cnm] = comcun[cnm] + Math.round(buildings[bnum][1][2][0][h]*(1 + 0.4*(level-1)));

}

for(var h = 0;h<buildings[bnum][1][3][0].length;h++){

cnm = cnum[buildings[bnum][1][3][1][h]];
compro[cnm] = compro[cnm] + Math.round(buildings[bnum][1][3][0][h]*(1 + 0.5*(level-1)));

}

var temp = psheet.split("=");
var buy = temp[1].split("|");
var sell = temp[0].split("|");
var stotal = 0;
var btotal = 0;

for(var i = 0;i<(buy.length-1);i++){

btotal = btotal + (compro[i]*buy[i]);

}

for(var i = 0;i<(sell.length-1);i++){

stotal = stotal + (comcun[i]*sell[i]);

}

var profit = btotal - stotal;
n = "<table><tr><th>Cost</th><th>Sales</th><th>Profit</th><th>Per</th></tr>";
n = n + "<tr><td>"+stotal+"</td><td>"+btotal+"</td><td>"+profit+"</td><td>Tick</td></tr>";
n = n + "<tr><td>"+(stotal*4)+"</td><td>"+(btotal*4)+"</td><td>"+(profit*4)+"</td><td>Day</td></tr>";
n = n + "<tr><td>"+(stotal*28)+"</td><td>"+(btotal*28)+"</td><td>"+(profit*28)+"</td><td>Week</td></tr>";
n = n + "</table>";

document.getElementById("PS_building_profit").innerHTML = n;

}

function sectorEconomyStats(){

var psheet = document.getElementById("temppl").innerHTML;
var temp = psheet.split("=");
var buy = temp[1].split("|");
var sell = temp[0].split("|");
var stotal = 0;
var btotal = 0;
var tcount = 0;

for(var i = 0;i<document.getElementsByTagName("td").length;i++){

if(document.getElementsByTagName("td")[i].getAttribute("onClick")!=null){

temp = document.getElementsByTagName("td")[i].innerHTML;

if(temp.search("transparent")==-1){




var compro = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var comcun = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

var name = document.getElementsByTagName("td")[i].childNodes[0].getAttribute("title");

if(name.search("Planet")!=-1||name.search("Starbase")!=-1){ }
else{

var spandata = document.getElementsByTagName("td")[i].childNodes[1].innerHTML;

for(var h = 0;h<buildings.length;h++){

if(name == buildings[h][0]){ bnum=h; break; }

}

var level = spandata;

for(var h = 0;h<buildings[bnum][1][2][0].length;h++){

cnm = cnum[buildings[bnum][1][2][1][h]];
comcun[cnm] = comcun[cnm] + Math.round(buildings[bnum][1][2][0][h]*(1 + 0.4*(level-1)));

}

for(var h = 0;h<buildings[bnum][1][3][0].length;h++){

cnm = cnum[buildings[bnum][1][3][1][h]];
compro[cnm] = compro[cnm] + Math.round(buildings[bnum][1][3][0][h]*(1 + 0.5*(level-1)));

}

for(var h = 0;h<(buy.length-1);h++){

btotal = btotal + (compro[h]*buy[h]);

}

for(var h = 0;h<(sell.length-1);h++){

stotal = stotal + (comcun[h]*sell[h]);

}

tcount = tcount + 1;

}

}


}

}

var profit = btotal - stotal;
n = "<br>Buildings: "+tcount+"<br><table><tr><th>Cost</th><th>Sales</th><th>Profit</th><th>Per</th></tr>";
n = n + "<tr><td>"+stotal+"</td><td>"+btotal+"</td><td>"+profit+"</td><td>Tick</td></tr>";
n = n + "<tr><td>"+(stotal*4)+"</td><td>"+(btotal*4)+"</td><td>"+(profit*4)+"</td><td>Day</td></tr>";
n = n + "<tr><td>"+(stotal*28)+"</td><td>"+(btotal*28)+"</td><td>"+(profit*28)+"</td><td>Week</td></tr>";
n = n + "</table>";

document.getElementById("total_profit_sector").innerHTML = n;



}

function importPL(other,loc){
if(other){ var code = loc; }
else{ var code = prompt("Paste your price list code here.",""); }

if(code != ''){

document.getElementById("temppl").innerHTML = code;
var temp = code.split("=");
var sell = temp[1].split("|");
var buy = temp[0].split("|");
buy[0] = buy[0].replace(" ","");

for(var i = 0;i<(buy.length-1);i++){

document.getElementById("pl_buy_"+i).value = buy[i];

}

for(var i = 0;i<(sell.length-1);i++){

document.getElementById("pl_sell_"+i).value = sell[i];

}

}

}

function exportPL(){

prompt("Save this code in a text document or on the offical Pardus Sector Tool website.",document.getElementById("temppl").innerHTML);

}